package pp.strategy.worktime;

import pp.IWorkTimeStrategy;
import properties.AutoBGProperties;
import properties.WorkTimeBean;
import util.WorkDayUtil;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class SmartWorkTimeStrategy implements IWorkTimeStrategy {

    @Override
    public Map<String, WorkTimeBean> workTime(AutoBGProperties autoBGProperties) {
        Map<String, WorkTimeBean> workTimeBeans = new HashMap<>(7);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;
        try {
            parse = sdf.parse(autoBGProperties.getFirstDay());
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        for (int i = 0; i < 7; i++) {
            Date date = new Date(parse.getTime() + (3600L * 24 * 1000) * i);
            try {
                //不是工作日跳过
                if (!WorkDayUtil.isWork(date)) {
                    continue;
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            WorkTimeBean weekBean = new WorkTimeBean("7.5", "0", autoBGProperties.getWorkTimes().getOne().getContext());
            workTimeBeans.put(sdf.format(date), weekBean);
        }

        return workTimeBeans;
    }
}
